//
//  YGHXManager.m
//  HXSDKDemo
//
//  Created by 黄德玉 on 2018/6/25.
//  Copyright © 2018年 none. All rights reserved.
//

#import "YGHXManager.h"
#import <HelpDesk/HelpDesk.h>
#import <Hyphenate/Hyphenate.h>

@implementation YGHXUserModel

@end

//////////////////////////////////////
//////////////////////////////////////

@interface YGHXManager()
@property (nonatomic,assign) BOOL hasSetupSuccess;
@property (nonatomic,strong) YGHXUserModel * hxUser;
@end

@implementation YGHXManager

+ (instancetype)sharedManger{
    static dispatch_once_t onceToken;
    static YGHXManager * manager = nil;
    dispatch_once(&onceToken, ^{
        manager = [[YGHXManager alloc] init];
        manager.hasSetupSuccess = NO;
        manager.hxUser = [[YGHXUserModel alloc] init];
        manager.hxUser.userName = @"ios";
        manager.hxUser.pssword = @"000000";
        [[NSNotificationCenter defaultCenter] addObserver:self
                                                 selector:@selector(appDidEnterBackgroundNotif:)
                                                     name:UIApplicationDidEnterBackgroundNotification
                                                   object:nil];
        
        [[NSNotificationCenter defaultCenter] addObserver:self
                                                 selector:@selector(appWillEnterForeground:)
                                                     name:UIApplicationWillEnterForegroundNotification
                                                   object:nil];
    });
    return manager;
}

- (BOOL)setup{
    if (self.hasSetupSuccess) {
        return true;
    }
    HOptions *option = [[HOptions alloc] init];
    option.appkey = YG_HX_APPKEY; // 必填项，appkey获取地址：kefu.easemob.com，“管理员模式 > 渠道管理 > 手机APP”页面的关联的“AppKey”
    option.tenantId = YG_HX_TENANTID;// 必填项，tenantId获取地址：kefu.easemob.com，“管理员模式 > 设置 > 企业信息”页面的“租户ID”
#if DEBUG
    option.enableConsoleLog = YES; // 是否打开日志信息
#endif
    //推送证书名字
    //    option.apnsCertName = @"your apnsCerName";//(集成离线推送必填)
    
    //Kefu SDK 初始化,初始化失败后将不能使用Kefu SDK
    HError *initError = [[HChatClient sharedClient] initializeSDKWithOptions:option];
    if (initError) { // 初始化错误
        self.hasSetupSuccess = NO;
    }else{
        self.hasSetupSuccess = YES;
        //初始化成功后，初始化表情包
        [[HDEmotionEscape sharedInstance] setEaseEmotionEscapePattern:@"\\[[^\\[\\]]{1,3}\\]"];
        [[HDEmotionEscape sharedInstance] setEaseEmotionEscapeDictionary:[HDConvertToCommonEmoticonsHelper emotionsDictionary]];
    }
    return self.hasSetupSuccess;
}



- (void)registerUserWithCallback:(void (^)(NSError *, id))callback{
    
}

- (BOOL)login{
    if ([HChatClient sharedClient].isLoggedInBefore) {
        return YES;
    }
    HError * err = [[HChatClient sharedClient] loginWithUsername:self.hxUser.userName password:self.hxUser.pssword];
    if (err) {
        return NO;
    }
    return YES;
}


- (BOOL)logout{
    HError * err = [[HChatClient sharedClient] logout:YES];
    if (err) {
        return NO;
    }
    return YES;
}

#pragma mark - notifiers
- (void)appDidEnterBackgroundNotif:(NSNotification*)notif{
    [[HChatClient sharedClient] applicationDidEnterBackground:notif.object];
}

- (void)appWillEnterForeground:(NSNotification*)notif{
    [UIApplication sharedApplication].applicationIconBadgeNumber = 0;
    [[HChatClient sharedClient] applicationWillEnterForeground:notif.object];
}


@end
